<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>014vue3响应式源码 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前言日志，各位看官就当乐子看吧。 正经人谁写日记啊？！！    ——鹅城县长 今天想进一步查看vue创建应用实例的流程，比如怎么实现响应式的、如何构建VNode的。。。下载了vuejs核心代码。主要看了响应式内容，其他问题参考文章中没写到，我自己看源码，一头雾水，没找到源头。 Vue3源码原文：学习Vue源码 文件中有个packages文件夹存放源代码，我们直接看这其中的目录结构： 1234567">
<meta property="og:type" content="article">
<meta property="og:title" content="014vue3响应式源码">
<meta property="og:url" content="http://example.com/2023/03/22/014vue3%E5%93%8D%E5%BA%94%E5%BC%8F%E6%BA%90%E7%A0%81/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前言日志，各位看官就当乐子看吧。 正经人谁写日记啊？！！    ——鹅城县长 今天想进一步查看vue创建应用实例的流程，比如怎么实现响应式的、如何构建VNode的。。。下载了vuejs核心代码。主要看了响应式内容，其他问题参考文章中没写到，我自己看源码，一头雾水，没找到源头。 Vue3源码原文：学习Vue源码 文件中有个packages文件夹存放源代码，我们直接看这其中的目录结构： 1234567">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://static.vue-js.com/c2344a60-cd86-11ea-ae44-f5d67be454e7.png">
<meta property="article:published_time" content="2023-03-22T13:42:24.815Z">
<meta property="article:modified_time" content="2023-03-24T09:02:56.951Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://static.vue-js.com/c2344a60-cd86-11ea-ae44-f5d67be454e7.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-014vue3响应式源码" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/03/22/014vue3%E5%93%8D%E5%BA%94%E5%BC%8F%E6%BA%90%E7%A0%81/" class="article-date">
  <time class="dt-published" datetime="2023-03-22T13:42:24.815Z" itemprop="datePublished">2023-03-22</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E7%99%BE%E6%97%A5%E5%8D%9A%E5%AE%A2%E8%AE%A1%E5%88%92/">百日博客计划</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      014vue3响应式源码
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>日志，各位看官就当乐子看吧。</p>
<p><strong>正经人谁写日记啊？！！</strong>    ——鹅城县长</p>
<p>今天想进一步查看<code>vue</code>创建应用实例的流程，比如怎么实现响应式的、如何构建VNode的。。。下载了<a target="_blank" rel="noopener" href="https://github.com/vuejs/core">vuejs核心代码</a>。主要看了响应式内容，其他问题参考文章中没写到，我自己看源码，一头雾水，没找到源头。</p>
<h1 id="Vue3源码"><a href="#Vue3源码" class="headerlink" title="Vue3源码"></a>Vue3源码</h1><p>原文：<a target="_blank" rel="noopener" href="https://vue3js.cn/global/">学习Vue源码</a></p>
<p>文件中有个<code>packages</code>文件夹存放源代码，我们直接看这其中的目录结构：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">// packages 文件夹</span><br><span class="line">.</span><br><span class="line">├── compiler-core // 顾名思义，核心中的核心，抽象语法树和渲染桥接实现</span><br><span class="line">├── compiler-dom // Dom的实现</span><br><span class="line">├── compiler-sfc // Vue单文件组件(.vue)的实现</span><br><span class="line">├── compiler-ssr</span><br><span class="line">├── global.d.ts</span><br><span class="line">├── reactivity</span><br><span class="line">├── runtime-core</span><br><span class="line">├── runtime-dom</span><br><span class="line">├── runtime-test</span><br><span class="line">├── server-renderer // 服务端渲染实现</span><br><span class="line">├── shared  // package 之间共享的工具库</span><br><span class="line">├── size-check</span><br><span class="line">├── template-explorer</span><br><span class="line">└── vue</span><br></pre></td></tr></table></figure>

<p>核心就是<code>compiler-core</code>、<code>compiler-dom</code>、<code>runtime-core</code>、<code>runtime-dom</code>、<code>reactivity</code></p>
<p><code>compiler-*</code>主要完成类似将<code>.vue -&gt; .html</code>的工作。<code>runtime-*</code>即是程序被编译了之后，打开程序并运行它直到程序关闭的这段时间的系列处理</p>
<h2 id="模块关系图"><a href="#模块关系图" class="headerlink" title="模块关系图"></a>模块关系图</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">                   +---------------------+    +----------------------+</span><br><span class="line">                   |                     |    |                      |</span><br><span class="line">     +------------&gt;|  @vue/compiler-dom  +---&gt;|  @vue/compiler-core  |</span><br><span class="line">     |             |                     |    |                      |</span><br><span class="line">+----+----+        +---------------------+    +----------------------+</span><br><span class="line">|         |</span><br><span class="line">|   vue   |</span><br><span class="line">|         |</span><br><span class="line">+----+----+        +---------------------+    +----------------------+    +-------------------+</span><br><span class="line">     |             |                     |    |                      |    |                   |</span><br><span class="line">     +------------&gt;|  @vue/runtime-dom   +---&gt;|  @vue/runtime-core   +---&gt;|  @vue/reactivity  |</span><br><span class="line">                   |                     |    |                      |    |                   |</span><br><span class="line">                   +---------------------+    +----------------------+    +-------------------+</span><br></pre></td></tr></table></figure>

<h2 id="响应式系统"><a href="#响应式系统" class="headerlink" title="响应式系统"></a>响应式系统</h2><p>源码在<code>reactivity</code>文件夹下，再看看其中的结构：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">// reactivity 文件夹</span><br><span class="line">.</span><br><span class="line">├── LICENSE</span><br><span class="line">├── README.md</span><br><span class="line">├── __tests__  // 单元测试目录</span><br><span class="line">│   ├── collections</span><br><span class="line">│   │   ├── Map.spec.ts</span><br><span class="line">│   │   ├── Set.spec.ts</span><br><span class="line">│   │   ├── WeakMap.spec.ts</span><br><span class="line">│   │   └── WeakSet.spec.ts</span><br><span class="line">│   ├── computed.spec.ts</span><br><span class="line">│   ├── effect.spec.ts</span><br><span class="line">│   ├── reactive.spec.ts</span><br><span class="line">│   ├── reactiveArray.spec.ts</span><br><span class="line">│   ├── readonly.spec.ts</span><br><span class="line">│   └── ref.spec.ts</span><br><span class="line">├── api-extractor.json</span><br><span class="line">├── index.js</span><br><span class="line">├── package.json</span><br><span class="line">└── src</span><br><span class="line">    ├── baseHandlers.ts // 基本类型的处理器</span><br><span class="line">    ├── collectionHandlers.ts  // Set Map WeakSet WeckMap的处理器</span><br><span class="line">    ├── computed.ts // 计算属性，同Vue2</span><br><span class="line">    ├── effect.ts // reactive 核心，处理依赖收集，依赖更新</span><br><span class="line">    ├── index.ts</span><br><span class="line">    ├── operations.ts // 定义依赖收集，依赖更新的类型</span><br><span class="line">    ├── reactive.ts // reactive 入口，内部主要以Proxy实现</span><br><span class="line">    └── ref.ts // reactive 的变种方法，Proxy处理不了值类型的响应，Ref来处理</span><br></pre></td></tr></table></figure>

<p>主要看<code>reactive.ts</code>、<code>baseHandlers.ts</code>、<code>effect.ts</code>、<code>ref.ts</code> 和 <code>computed.ts</code>。并且建议按照这个顺序阅读。有个流程图方便大家理解。</p>
<p><img src="https://static.vue-js.com/c2344a60-cd86-11ea-ae44-f5d67be454e7.png" alt="reactivity流程图"></p>
<p>我看到<code>effect</code>的解析，然后就看不下去了，不知道代码具体在干嘛。之后再找找其他人写的博客。</p>
<h1 id="彩蛋"><a href="#彩蛋" class="headerlink" title="彩蛋"></a>彩蛋</h1><p>设想的结果太完美，预计自己做不好一件事，就会倾向于逃避完成。负反馈的完美主义，真不行呐！</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2023/03/22/014vue3%E5%93%8D%E5%BA%94%E5%BC%8F%E6%BA%90%E7%A0%81/" data-id="clfjqowue000dewaa00lj9k32" data-title="014vue3响应式源码" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/03/22/015%E7%9B%B8%E7%AD%89%E3%80%81%E6%8B%B7%E8%B4%9D/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          015相等、拷贝
        
      </div>
    </a>
  
  
    <a href="/2023/03/22/013%E5%89%8D%E7%AB%AF/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">013前端</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%99%BE%E6%97%A5%E5%8D%9A%E5%AE%A2%E8%AE%A1%E5%88%92/">百日博客计划</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/ES6/" rel="tag">ES6</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/uniapp/" rel="tag">uniapp</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag">前端</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/ES6/" style="font-size: 10px;">ES6</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/uniapp/" style="font-size: 10px;">uniapp</a> <a href="/tags/%E5%89%8D%E7%AB%AF/" style="font-size: 10px;">前端</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/03/">March 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/03/24/025uniapp-done/">025uniapp-done</a>
          </li>
        
          <li>
            <a href="/2023/03/23/024uniapp%E7%AC%AC%E4%B8%89%E5%A4%A9/">024uniapp第三天</a>
          </li>
        
          <li>
            <a href="/2023/03/22/023gitee-hexo%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/">023gitee+hexo搭建个人博客</a>
          </li>
        
          <li>
            <a href="/2023/03/22/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8MDN%E6%96%87%E6%A1%A3/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/03/22/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>